from Selenium2Library.keywords.keywordgroup import KeywordGroup
from Selenium2Library.locators import ElementFinder

class _BreezeAccountSummaryKeywords(KeywordGroup):
    """Library for Breeze See Account Summary Keywords purposes.
    
    This library is used in Breeze projects.
    """
    def __init__(self):
        """Does nothing. init only"""
        print '_BreezeAccountSummaryKeywords.__init__'
        
    def read_bmw_accounts(self):
        """Read BMW Accounts list for comparing with Accounts in Ibanking.
        
        See `Read Ibnk Accounts`
        
        "See" list comparing.
        """
        return self._run('read_bmw_accounts')

    def _read_bmw_accounts_hk(self):
        ''' Return a list of all accounts, cards and loans from BMW.
        '''
        names = self._element_find("//div[@id='main']/div/dl/dd/a/span[1]", False, False)
        numbers = self._element_find("//div[@id='main']/div/dl/dd/a/span[2]", False, False)
        amounts = self._element_find("//div[@id='main']/div/dl/dd/a/span[3]", False, False)
        
        accounts = []
        for i in xrange(len(names)): #TODO
            accounts.append([names[i].text,
                             numbers[i].text.split(' ')[0],
                             amounts[i].text[:3],
                             amounts[i].text[3:]])
        self._info(len(accounts))
        return accounts

    def _read_bmw_accounts_pk(self):
        names = self._element_find("//div[@id='main']/div/dl/dd/a/span[1]", False, False)
        numbers = self._element_find("//div[@id='main']/div/dl/dd/a/span[2]", False, False)
        amounts = self._element_find("//div[@id='main']/div/dl/dd/a/span[3]", False, False)
        
        accounts = []
        for i in xrange(len(names)): #TODO
            accounts.append([names[i].text,
                            numbers[i].text,
                            amounts[i].text[:3],
                            amounts[i].text[3:]])
        self._info(len(accounts))
        return accounts

    def _read_bmw_accounts_id(self):
        '''For BMW ID, please refer to BMW PK if not work here
        '''
        names = self._element_find("//div[@id='main']/div/dl/dd/a/span[1]", False, False)
        numbers = self._element_find("//div[@id='main']/div/dl/dd/a/span[2]", False, False)
        amounts = self._element_find("//div[@id='main']/div/dl/dd/a/span[3]", False, False)
        
        accounts = []
        for i in xrange(len(names)): #TODO
            accounts.append([names[i].text,
                            numbers[i].text,
                            amounts[i].text[:3],
                            amounts[i].text[3:]])
        self._info(len(accounts))
        return accounts
    
    def read_ibnk_accounts(self):
        """Read iBanking Accounts list for comparing with Accounts in BMW.
        
        See `Read BMW Accounts`
        
        "See" list comparing.
        """
        return self._run('read_ibnk_accounts')

    def _read_ibnk_accounts_hk(self):
        ''' Return a list of all accounts, cards and loans from iBanking.
        '''
        rows = self._element_find("//table[@class='tbl_listing']/tbody/tr[@class='tbl_listing_row_even' or @class='tbl_listing_row_odd']", False, False)
        
        accounts = []
        for r in rows: #TODO
            name = r.find_elements_by_xpath("./td[2]/a")
            number = r.find_elements_by_xpath("./td[3]")
            currencie = r.find_elements_by_xpath("./td[4]")
            curr_bal = r.find_elements_by_xpath("./td[5]")
            ava_bal = r.find_elements_by_xpath("./td[7]")
            self._info('%s> %s>' % (ava_bal[0].text[:-3], curr_bal[0].text[:-3]))
            if not ava_bal[0].text[:-3].strip() == '':
                balance = ava_bal[0].text[:-3].strip()
            else:
                balance = curr_bal[0].text[:-3].strip()
            accounts.append([name[0].text,
                             number[0].text,
                             currencie[0].text.strip(),
                             balance])
        self._info(len(accounts))
        return accounts
         
    def _read_ibnk_accounts_pk(self):
        ''' Return a list of all accounts, cards and loans from iBanking.
        '''
        rows = self._element_find("//table[@class='tbl_listing']", False, False)

        accountlist = []
        cardlist = []
        loanlist = []
        accounts = []
        
        for r in rows: #TODO
            if r.find_elements_by_xpath("./tbody/tr[1]/th[2]")[0].text == 'Accounts':
                accountlist = r.find_elements_by_xpath("./tbody/tr[@class='tbl_listing_bottom']")
            if r.find_elements_by_xpath("./tbody/tr[1]/th[2]")[0].text == 'Cards':
                cardlist = r.find_elements_by_xpath("./tbody/tr[position()>1 and position()<last()]")
            if r.find_elements_by_xpath("./tbody/tr[1]/th[2]")[0].text == 'Loans & Saadiq Finance':
                loanlist = r.find_elements_by_xpath("./tbody/tr[position()>1 and position()<last()-1]")
        
        for r in accountlist: #get accounts list
            name = r.find_elements_by_xpath("./td[2]/a")
            number = r.find_elements_by_xpath("./td[3]/a")
            currencie = r.find_elements_by_xpath("./td[4]")
            curr_bal = r.find_elements_by_xpath("./td[7]")
            ava_bal = r.find_elements_by_xpath("./td[5]")
            if not curr_bal[0].text[:-3].strip() == '':
                balance = curr_bal[0].text[:-3].strip()
            else:
                balance = ava_bal[0].text[:-3].strip()
            accounts.append([name[0].text,
                             number[0].text,
                             currencie[0].text.strip(),
                             balance])
        for r2 in cardlist: #get cards list
            name = r2.find_elements_by_xpath("./td[2]/a")
            number = r2.find_elements_by_xpath("./td[3]/a")
            currencie = r2.find_elements_by_xpath("./td[4]")
            balance = r2.find_elements_by_xpath("./td[5]")[0].text[4:-3]
            accounts.append([name[0].text,
                            number[0].text,
                            currencie[0].text.strip(),
                            balance])
        for r3 in loanlist: #get loans list
            name = r3.find_elements_by_xpath("./td[2]/a")
            number = r3.find_elements_by_xpath("./td[3]/a")
            currencie = r3.find_elements_by_xpath("./td[4]")[0].text[:3]
            balance = r3.find_elements_by_xpath("./td[4]")[0].text[4:-3]
            accounts.append([name[0].text,
                            number[0].text,
                            currencie,
                            balance])
        self._info(len(accounts))
        return accounts

    def _read_ibnk_accounts_id(self):
        #select to links frame for clicking "Accounts" hyperlink to see Accounts list
        self.select_links_frame_ibanking()
        self.current_frame_contains("Accounts")
        
        element = self._element_find("//a[@id='nav_ML4']", True, False)
        self._info(element)
        element.click()
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Please click on \"Account Number\" to view details.")
        
        rows = self._element_find("//*[@id='acclist']/tbody/tr", False, False)
        self._info(rows)
        
        accounts = []
        
        #accounts records
        for r in rows: #TODO
            type = r.find_elements_by_xpath("./td[1]")
            number = r.find_elements_by_xpath("./td[2]")
            avail = r.find_elements_by_xpath("./td[3]")
            ledger = r.find_elements_by_xpath("./td[4]")
            self._info('%s> %s> %s> %s>' % (type[0].text, number[0].text, avail[0].text, ledger[0].text))
            
            accounts.append([type[0].text,
                             number[0].text,
                             ledger[0].text.split(' ')[1],
                             ledger[0].text.split(' ')[2]])
        self._info(len(accounts))
        
        #credit cards records
        self.select_links_frame_ibanking()
        element = self._element_find("//*[@id='nav_ML300']", True, False)
        self._info(element)
        element.click()
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Credit Cards")
        
        rows2 = self._element_find("//tr[@class='table_row_white']", False, False)
        for r2 in rows2: #TODO
            type = r2.find_elements_by_xpath("./td[1]")
            number = r2.find_elements_by_xpath("./td[2]")
            bonus = r2.find_elements_by_xpath("./td[3]")

            self._info('%s> %s> %s>' % (type[0].text, number[0].text, bonus[0].text))
            #balance = r2.find_elements_by_xpath("./td[5]")[0].text[4:-3]
            accounts.append([type[0].text,
                            number[0].text,
                            bonus[0].text.strip()])
        self._info(len(accounts))
        
        #do not implement "Loan" part as no data when writing script.
        
        return accounts


